package com.controller;

import com.Util.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.entity.User;
import com.entity.base.Result;
import com.service.UserService;
import com.service.impl.UserServiceImpl;
import com.vo.TokenVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping(value = "/user")
@Slf4j
@Api(tags = "(学生,教师,管理员)通用相关接口", description = "提供相关的 Rest API")
public class UserController extends BaseController<User,UserService>{


    @Autowired
    UserService userService;

    UserServiceImpl userServiceImpl;

    @PostMapping("/login")
    @ApiOperation("用户登录接口")
    public Result login(@RequestBody User foUser) throws NoSuchAlgorithmException {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", foUser.getUsername());
        User user = userService.getOne(wrapper);
        if (user != null) {
            String newPwd = SaltEncryption.saltEncryption(foUser.getPassword(), user.getSalt());
            //对用户输入的密码加密后 对比数据库的密码 并且用户的状态是正常的
            if (newPwd.equals(user.getPassword()) && user.getStatus() == 1) {
                //发放token令牌
                String token = TokenUtils.createToken(new TokenVo(user.getRoleId() + "", user.getUsername(), newPwd));
                return Result.success(token);
            } else {//密码错误 或者 账号封禁
                return Result.error("账号密码错误,或用户状态异常");
            }
        } else return Result.error("用户不存在");
    }

    @ApiOperation("用户注册接口")
    @PostMapping("/register")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user", value = "系统用户实体", required = true, dataType = "user", paramType = "body")
    })
    public Result Register(@RequestBody User user) throws NoSuchAlgorithmException {
        //盐值
        String salt = UUID.randomUUID().toString().substring(0, 6);
        String newPwd = SaltEncryption.saltEncryption(user.getPassword(), salt);
        user.setPassword(newPwd);
        user.setSalt(salt);
        //设置加密
        userService.save(user);
        //发放token令牌
        String token = TokenUtils.createToken(new TokenVo("1", user.getUsername(), newPwd));
        return Result.success(token);
    }

    @ApiOperation("获取token接口")
    @GetMapping("/checkToken")
    public Result checkToken(HttpServletRequest request) {
        TokenVo tokenVo = new CheckToken().checkToken(request,userServiceImpl);
        if (tokenVo != null) {
            return Result.success(tokenVo);
        } else {
            return Result.error("token无效");
        }
    }


}
